package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemBatchRecordDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.ItemBatchRecordStatusEnum;
import com.jyu.apts.service.ItemBatchRecordService;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationItemBatchRecordController {

	@Autowired
	ItemBatchRecordService itemBatchRecordService;
	@Autowired
	ItemProductionLineService itemProductionLineService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/organizationItemBatchRecord_html")
	public ModelAndView organizationItemBatchRecord_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationItemBatchRecord.html");
		return mv;
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/addOrganizationItemBatchRecord")
	public ResultDto addOrganizationItemBatchRecord(long count,long itemProductionLineId) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		if(itemProductionLine == null) {
			return new ResultDto("生产线不存在",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(itemProductionLine.getItem().getOrganization().getId() != organization.getId()) {
			return new ResultDto("非本机构生产线",null,2);
		}
		ItemBatchRecord itemBatchRecord = new ItemBatchRecord();
		itemBatchRecord.setCount(count);
		itemBatchRecord.setItemProductionLine(itemProductionLine);
		itemBatchRecordService.save(itemBatchRecord);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/upOrganizationItemBatchRecord")
	public ResultDto upOrganizationItemBatchRecord(long id,long count,long itemProductionLineId) {
		ItemBatchRecord itemBatchRecord = itemBatchRecordService.getOne(id);
		if(itemBatchRecord == null) {
			return new ResultDto("商品批次不存在!",null,2);
		}
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		if(itemProductionLine == null) {
			return new ResultDto("生产线不存在",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(itemProductionLine.getItem().getOrganization().getId() != organization.getId()) {
			return new ResultDto("非本机构生产线",null,2);
		}
		itemBatchRecord.setItemProductionLine(itemProductionLine);
		itemBatchRecord.setCount(count);
		itemBatchRecordService.save(itemBatchRecord);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/delOrganizationItemBatchRecord")
	public ResultDto delOrganizationItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			if(itemBatchRecord.getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构商品批次",null,2);
			}
		}
		itemBatchRecordService.delete(itemBatchRecords);
		return new ResultDto("删除成功",null,0);
	}
	
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/stopOrganizationItemBatchRecord")
	public ResultDto stopOrganizationItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			if(itemBatchRecord.getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构商品批次",null,2);
			}
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.STOPED.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("暂停成功",null,0);
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/finishOrganizationItemBatchRecord")
	public ResultDto finishOrganizationItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			if(itemBatchRecord.getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构商品批次",null,2);
			}
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.FINISHED.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("完成成功",null,0);
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/workOrganizationItemBatchRecord")
	public ResultDto workOrganizationItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			if(itemBatchRecord.getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构商品批次",null,2);
			}
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.WORKING.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("启动成功",null,0);
	}
	
	@RequiresPermissions("organizationItemBatchRecord_html")
	@RequestMapping("/getOrganizationItemBatchRecordPage")
	public TableDto<ItemBatchRecordDto> getOrganizationItemBatchRecordPage(Integer pageNumber,Integer pageSize,long itemProductionLineId,Integer status,String search) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(itemProductionLine.getItem().getOrganization().getId() != organization.getId()) {
			return null;
		}
		Page<ItemBatchRecord> pageResult = itemBatchRecordService.getItemBatchRecordPage(pageNumber, pageSize, itemProductionLineId,status, search);
		List<ItemBatchRecord> organizationItemBatchRecords = pageResult.getContent();
		List<ItemBatchRecordDto> organizationItemBatchRecordDtos = new ArrayList<ItemBatchRecordDto>();
		organizationItemBatchRecords.forEach(organizationItemBatchRecord->organizationItemBatchRecordDtos.add(new ItemBatchRecordDto().convert(organizationItemBatchRecord)));
		return new TableDto<ItemBatchRecordDto>(organizationItemBatchRecordDtos,pageResult.getTotalElements());
	}
	
}
